export class Loadglb {
  constructor(asm) {
    this.asm = asm;
  }
  add_obj(name) {
    // 可以反复加很多meshtask
    let ob = this.asm.addMeshTask(name, null, "/static/stl/", `${name}.glb`);

    let start = 0;
    ob.onSuccess = function (task) {
      task.loadedMeshes.forEach(function (mesh) {
        // mesh.name=`${name}_${start}`;
        mesh.name = `${name}`;
        start = start + 1;
      });

      return ob;
    };
  }

  add_gui(scene) {
    // GUI
    var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI(
      "UI"
    );

    var panel = new BABYLON.GUI.StackPanel();
    advancedTexture.addControl(panel);

    var onButtonClick = function (which) {
      console.log(444);
      console.log(which);
      let box_mesh = scene.getMeshByName("box");
      console.log(box_mesh.name);
      console.log(box_mesh.position);
      box_mesh.position=new BABYLON.Vector3(5,5,0);
      console.log(box_mesh.position);
      let floor_mesh=scene.getMeshByName("floor");
      console.log(floor_mesh.name);
      floor_mesh.position=new BABYLON.Vector3(0,20,0);
      console.log(floor_mesh.position);


    };

    var button = BABYLON.GUI.Button.CreateSimpleButton("but", "Click Me");
    button.width = 0.2;
    button.height = "40px";
    button.color = "white";
    button.background = "green";

    button.isPointerOver = false;

    button.onPointerDownObservable.add(function () {
      textblock.text = "Down!";
    });
    button.onPointerUpObservable.add(function () {
      textblock.text = "Up!";
      if (!button.isPointerOver) return;
      onButtonClick(button);
    });
    button.onPointerEnterObservable.add(function () {
      textblock.text = "Enter!";
      button.isPointerOver = true;
    });
    button.onPointerOutObservable.add(function () {
      textblock.text = "Out!";
      button.isPointerOver = false;
    });
    button.onPointerMoveObservable.add(function (coordinates) {
      var relative = button.getLocalCoordinates(coordinates);
      textblock.text = relative.x + ", " + relative.y;
    });

    panel.addControl(button);

    var textblock = new BABYLON.GUI.TextBlock("textblock", "");
    textblock.width = 1;
    textblock.height = "40px";
    textblock.color = "white";
    panel.addControl(textblock);
  }
}
